package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.entity.Result;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfoVo;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderSettingMapper orderSettingMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public Result submit(OrderInfoVo orderInfoVo) {
        Date orderDate = orderInfoVo.getOrderDate();
        String telephone = orderInfoVo.getTelephone();
        String name = orderInfoVo.getName();
        Integer sex = orderInfoVo.getSex();
        String idCard = orderInfoVo.getIdCard();
        Integer setmealId = orderInfoVo.getSetmealId();
        String orderType = orderInfoVo.getOrderType();

        //1.根据预约日期查询是否存在预约设置
        OrderSetting orderSetting = orderSettingMapper.findByDate(orderDate);
        if (null == orderSetting) {
            return Result.error("没有档期");
        }
        //2.查看是否约满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (number <= reservations) {
            return Result.error("预约已满");
        }
        //3.根据手机号码查询是否是我们的会员
        Member member = memberMapper.findByTelephone(telephone);
        //4.如果不是就自动注册
        member.setName(name);
        member.setSex(String.valueOf(sex));
        member.setIdCard(idCard);
        member.setPhoneNumber(telephone);
        member.setRegTime(new Date());
        memberMapper.update(member);//一定要返回主键
        Integer memberId = member.getId();
        //5.根据会员id、套餐id、预约日期是否已经存在预约信息
        Order order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
        order.setOrderType(orderType);
        order.setSetmealId(setmealId);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        List<Order> orders = orderMapper.findByCondition(order);
        if (CollectionUtil.isNotEmpty(orders)) {
            return Result.error("不要重复预约");
        }
        //6.直接下单
        orderMapper.add(order);
        //7.已预约人数加+1
        orderSetting.setReservations(reservations + 1);
        int i = orderSettingMapper.editReservations(orderSetting);
        if (0 == i) {
            throw new RuntimeException("更新失败");
        }

        return Result.success("", order);//页面需要使用订单id
    }

    @Override
    public Map findById(Integer id) {
        return orderMapper.findById(id);
    }

}
